﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;
using NetCMS.Content.Common;

public partial class manage_channel_Classa_dd : NetCMS.Web.UI.ManagePage
{
    public manage_channel_Classa_dd()
    {
        this.Authority_Code = "D004";
    }
    Channel rd = new Channel();
    rootPublic pd = new rootPublic();
    public static string ClasssavePath = string.Empty;
    public static string dirm = NetCMS.Config.UIConfig.dirDumm;
    public static string ChannelNamenavi = string.Empty;
    public static string SiteDomain = NetCMS.Publish.CommonData.getUrl();
    public static string GetChID = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (SiteID != "0")
            {
                PageError("分站没权限操作", "javascript:history.back();", true);
            }
            copyright.InnerHtml = CopyRight;
            if (dirm.Trim() != string.Empty) { dirm = "/" + dirm; }
            string ChID = Request.QueryString["ChID"];
            GetChID = ChID;
            string ClassID = Request.QueryString["ClassID"];
            string Action = Request.QueryString["Action"];
            if (ClassID != string.Empty && ClassID != null)
            {
                gParentID.InnerHtml = rd.getClassName(int.Parse(ClassID.ToString()));
                ParanetID.Value = ClassID.ToString();
                gClassID.Value = ClassID.ToString();
            }
            else
            {
                gParentID.InnerHtml = "根栏目";
                ParanetID.Value = "0";
            }
            if (ChID == string.Empty || ChID == null)
            {
                PageError("错误的参数", "javascript:history.back()", true);
            }
            else
            {
                if (Action != null && Action != string.Empty)
                {
                    this.gAction.Value = Request.QueryString["Action"];
                    IDataReader dc = rd.GetClassInfo(int.Parse(ClassID));
                    if (dc.Read())
                    {
                        gParentID.InnerHtml = rd.getClassName(int.Parse(dc["ParentID"].ToString()));
                        if (dc["isPage"].ToString() == "1")
                        {
                            this.isPage.Checked = true;
                        }
                        this.PageContent.Value = dc["PageContent"].ToString();
                        this.isPage.Enabled = false;
                        this.classCName.Text = dc["classCName"].ToString();
                        this.classEName.Text = dc["classEName"].ToString();
                        this.OrderID.Text = dc["OrderID"].ToString();
                        this.Templet.Text = dc["Templet"].ToString();
                        this.SavePath.Text = dc["SavePath"].ToString();
                        this.FileName.Text = dc["FileName"].ToString();
                        this.ContentTemplet.Text = dc["ContentTemplet"].ToString();
                        this.ContentSavePath.Text = dc["ContentSavePath"].ToString();
                        this.ContentFileNameRule.Text = dc["ContentFileNameRule"].ToString();
                        if (dc["isShowNavi"].ToString() == "1")
                        {
                            this.isShowNavi.Checked = true;
                        }
                        this.NaviContent.Text = dc["NaviContent"].ToString();
                        this.KeyMeta.Text = dc["KeyMeta"].ToString();
                        this.DescMeta.Text = dc["DescMeta"].ToString();
                        this.PicURL.Text = dc["PicURL"].ToString();
                        if (dc["isLock"].ToString() == "1")
                        {
                            this.isLock.Checked = true;
                        }
                        this.ClassNavi.Text = dc["ClassNavi"].ToString();
                        this.ContentNavi.Text = dc["ContentNavi"].ToString();
                        this.UserPop1.AuthorityType = int.Parse(dc["isDelPoint"].ToString());
                        this.UserPop1.Point = int.Parse(dc["iPoint"].ToString());
                        this.UserPop1.Gold = int.Parse(dc["Gpoint"].ToString());
                        this.UserPop1.MemberGroup = dc["GroupNumber"].ToString().Split(',');
                    }
                    dc.Close();
                    IDataReader gCdr = rd.ChannelInfo(int.Parse(ChID.ToString()));
                    if (gCdr.Read())
                    {
                        ChannelName.InnerHtml = gCdr["channelName"].ToString();
                    }
                    gCdr.Close();
                }
                else
                {
                    this.Authority_Code = "D004";
                    this.CheckAdminAuthority();
                    IDataReader Cdr = rd.ChannelInfo(int.Parse(ChID.ToString()));
                    if (Cdr.Read())
                    {
                        ChannelName.InnerHtml = Cdr["channelName"].ToString();
                        string Channelurls = "/" + Cdr["htmldir"].ToString() + "/" + Cdr["indexFileName"].ToString() + "";
                        if (Cdr["isDelPoint"].ToString() != "0")
                        {
                            Channelurls = "/list.aspx?id=" + Cdr["id"].ToString() + "&ChID=" + Request.QueryString["ChID"] + "";
                        }
                        ChannelNamenavi = "<a href=\"" + NetCMS.Publish.CommonData.getUrl() + Channelurls.Replace("//", "/") + "\">" + Cdr["channelName"].ToString() + "</a>";
                        if (NetCMS.Config.UIConfig.dirHtml == "")
                        {
                            ClasssavePath = NetCMS.Config.UIConfig.dirHtml + "/" + Cdr["channelEItem"].ToString();
                        }
                        else
                        {
                            ClasssavePath = "/" + NetCMS.Config.UIConfig.dirHtml + "/" + Cdr["channelEItem"].ToString();
                        }
                                               
                        string ClassTemplet = "/{@dirTemplet}/" + Cdr["TempletPath"].ToString() + "/" + Cdr["classtemplet"].ToString();
                        string ConentTemplet = "/{@dirTemplet}/" + Cdr["TempletPath"].ToString() + "/" + Cdr["newstemplet"].ToString();
                        this.Templet.Text = ClassTemplet.Replace("//", "/");
                        this.SavePath.Text = Cdr["ClassSave"].ToString().Replace("//", "/");
                        this.ContentTemplet.Text = ConentTemplet.Replace("//", "/");
                        this.ContentSavePath.Text = Cdr["SavePath"].ToString();
                        this.ContentFileNameRule.Text = Cdr["FileName"].ToString();
                    }
                    Cdr.Close();
                }
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int ParentID = int.Parse(this.ParanetID.Value);
            string gID = Request.QueryString["ClassID"];
            int ID = 0;
            if (gID != string.Empty && gID != null)
            {
                ID = int.Parse(Request.QueryString["ClassID"]); 
            }
            int ChID = int.Parse(Request.QueryString["ChID"]);
            string classCName = this.classCName.Text;
            string classEName = this.classEName.Text;
            //判断英文名称是否重复
            if (rd.getClassInfoCord(classEName, ID) > 0)
            {
                PageError("英文名称[" + classEName + "]已经存在！", "javascript:history.back();", true);
            }
            int OrderID = 0;
            if (NetCMS.Common.Input.IsInteger(this.OrderID.Text))
            {
                OrderID = int.Parse(this.OrderID.Text);
                if (OrderID > 10)
                {
                    OrderID = 10;
                }
            }
            int isPage = 0;
            if (this.isPage.Checked)
            {
                isPage = 1;
            }
            string PageContent = this.PageContent.Value;
            string Templet = this.Templet.Text;
            string ContentTemplet = this.ContentTemplet.Text;
            string SavePath = this.SavePath.Text;
            string FileName = this.FileName.Text;
            string ContentSavePath = this.ContentSavePath.Text;
            string ContentFileNameRule = this.ContentFileNameRule.Text;
            if (ContentFileNameRule.Trim() == string.Empty)
            {
                PageError("请填写内容文件名规则！", "javascript:history.back();", true);
            }
            if (ContentFileNameRule.IndexOf(".") == -1)
            {
                PageError("请填写内容文件名规则必须包含扩展名！", "javascript:history.back();", true);
            }
            if (FileName.IndexOf(".") == -1)
            {
                PageError("请填写栏目生成文件名必须包含扩展名！", "javascript:history.back();", true);
            }
            int isShowNavi = 0;
            if (this.isShowNavi.Checked)
            {
                isShowNavi = 1;
            }
            string NaviContent = this.NaviContent.Text;
            string KeyMeta = this.KeyMeta.Text;
            string DescMeta = this.DescMeta.Text;
            string PicURL = this.PicURL.Text;
            int isDelPoint = this.UserPop1.AuthorityType;
            int Gpoint = this.UserPop1.Gold;
            int iPoint = this.UserPop1.Point;
            string[] _GroupNumber = this.UserPop1.MemberGroup;
            string GroupNumber = "";
            foreach (string gnum in _GroupNumber)
            {
                if (GroupNumber != "")
                    GroupNumber += ",";
                GroupNumber += gnum;
            }
            int isLock = 0;
            if(this.isLock.Checked)
            {
                isLock=1;
            }
            string ClassNavi = this.ClassNavi.Text;
            string ContentNavi = this.ContentNavi.Text;
            DateTime _Temp_date = System.DateTime.Now;
            NetCMS.Model.ChannelClassInfo uc = new NetCMS.Model.ChannelClassInfo();
            uc.Id = ID;
            uc.ParentID = ParentID;
            uc.ChID = ChID;
            uc.classCName = classCName;
            uc.classEName = classEName;
            uc.OrderID = OrderID;
            uc.isPage = isPage;
            uc.PageContent = PageContent;
            uc.Templet = Templet;
            uc.ContentTemplet = ContentTemplet;
            uc.SavePath = pd.getResultPage(SavePath, _Temp_date, "0", classEName);
            uc.FileName = pd.getResultPage(FileName, _Temp_date, "0", classEName);
            uc.ContentSavePath = ContentSavePath;
            uc.ContentFileNameRule = ContentFileNameRule;
            uc.isShowNavi = isShowNavi;
            uc.NaviContent = NaviContent;
            uc.KeyMeta = KeyMeta;
            uc.DescMeta = DescMeta;
            uc.PicURL = PicURL;
            uc.isDelPoint = isDelPoint;
            uc.Gpoint = Gpoint;
            uc.iPoint = iPoint;
            uc.GroupNumber = GroupNumber;
            uc.isLock = isLock;

            uc.ClassNavi = ClassNavi.ToLower().Replace("{@parentclassstr}", ParentClass(ID));
            uc.ContentNavi = ContentNavi.ToLower().Replace("{@parentclassstr}", ParentClass(ID)).Replace("{@classurl}", ClassURL(pd.getResultPage(SavePath, _Temp_date, "0", classEName) + "/" + pd.getResultPage(FileName, _Temp_date, "0", classEName), isDelPoint));
            uc.SiteID = SiteID;
            if (Request.QueryString["Action"] != null && Request.QueryString["Action"] != string.Empty)
            {
                rd.updateClassInfo(uc);
                PageRight("修改栏目成功", "class_list.aspx?ChID=" + ChID.ToString() + "", true);
            }
            else
            {
                rd.insertClassInfo(uc);
                PageRight("添加栏目成功", "class_list.aspx?ChID=" + ChID.ToString() + "", true);
            }
        }
    }
    /// <summary>
    /// 得到栏目连接地址
    /// </summary>
    /// <returns></returns>
    protected string ClassURL(string URLs,int isDel)
    {
        string Urlstr = string.Empty;
        URLs = URLs.Replace("//", "/");
        if (dirm.Trim() != string.Empty) { dirm = "/" + dirm; }
        if (isDel != 0)
        {
            Urlstr = dirm + "/list.aspx?id=" + (rd.GetTopClassID() + 1) + "&ChID=" + Request.QueryString["ChID"] + "";
        }
        else
        {
            Urlstr = dirm + "/" + URLs;
        }
        return NetCMS.Publish.CommonData.getUrl() + Urlstr.Replace("//","/");
    }

    /// <summary>
    /// 得到父栏目相关
    /// </summary>
    /// <param name="ClassID"></param>
    /// <returns></returns>
    protected string ParentClass(int ClassID)
    {
        string _Str = string.Empty;
        if (dirm.Trim() != string.Empty){dirm = "/" + dirm;}
        string urls = string.Empty;
        if (ClassID != 0)
        {
            IDataReader dr = rd.GetClassInfo(ClassID);
            if (dr.Read())
            {
                string tmsavebstr = dirm + "/" + dr["SavePath"].ToString() + "/" + dr["FileName"].ToString();
                if (dr["isDelPoint"].ToString() != "0")
                {
                    tmsavebstr = dirm + "/list.aspx?id=" + dr["id"].ToString() + "&ChID=" + Request.QueryString["ChID"] + "";
                }
                _Str += "<a href=\"" + tmsavebstr.Replace("//", "/") + "\">" + dr["classCName"] + "</a> >> ";
                if (dr["ParentID"] != DBNull.Value && dr["ParentID"].ToString() != "0")
                {
                    IDataReader dr2 = rd.GetClassInfo(int.Parse(dr["ParentID"].ToString()));
                    while (dr2.Read())
                    {
                        urls = dirm + "/" + dr2["SavePath"].ToString() + "/" + dr2["FileName"].ToString();
                        if (dr2["isDelPoint"].ToString() != "0")
                        {
                            urls = dirm + "/list.aspx?id=" + dr2["id"].ToString() + "&ChID=" + Request.QueryString["ChID"] + "";
                        }
                        _Str = "<a href=\"" + urls.Replace("//", "/") + "\">" + dr2["classCName"] + "</a> >> " + _Str;
                        _Str = ParentClass(int.Parse(dr2["ParentID"].ToString())) + _Str;
                    }
                    dr2.Close();
                }
            }
            dr.Close();
        }
        return _Str;
    }
}
